const { defineConfig } = require('@vue/cli-service')
const Mock = require('mockjs')

const arr = [
  '热门推荐',
  '手机数码',
  '京东超市',
  '家用电器',
  '电脑办公',
  '玩乐乐器',
  '家具厨具',
  '家具家装',
  '内衣配饰',
]

const data = Mock.mock({
  'list|9': [
    {
      id: '@id',
      'type|+1': arr,
      'children|30': [
        {
          id: '@id',
          goods: '@ctitle(3)',
          img: '@image(50x50,@color, @city)',
        },
      ],
    },
  ],
})

const city = Mock.mock({
  'list|10': [
    {
      id: '@id',
      province: '@province',
      selectedArr: [],
      'children|20': [
        {
          id: '@guid',
          city: '@city',
          check: false,
        },
      ],
    },
  ],
})

module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares: (middlewares, { app }) => {
      //

      app.get('/api/city', (req, res) => {
        res.send({
          code: 1,
          msg: 'success',
          data: city.list,
        })
      })

      app.get('/api/list', (req, res) => {
        const { type } = req.query
        if (type) {
          res.send({
            code: 1,
            msg: 'success',
            data: {
              typeArr: arr,
              list: data.list.filter((v) => v.type === type),
            },
          })
        } else {
          res.send({
            code: -1,
            msg: 'fail',
          })
        }
      })
      return middlewares
    },
  },
})
